﻿using DataModels.Dto;
using DataModels.Dto.Home;
using DataModels.Entities.Base;
using DataModels.Entities.Community;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;

namespace Services.Community
{
    public class HomeLettersService(SqlSugarClient db) : BaseService(db)
    {
        /// <summary>
        /// 获取家书列表
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public ResponseModel GetHomeLetters(Guid? ProjectId, DateTime? StartTime, DateTime? EndTime)
        {
            var res = db.Queryable<HomeLetters>()
                .Where(hl=> hl.Status == DataModels.Enums.GeneralEnum.Status.Enable)
                .WhereIF(ProjectId.HasValue, hl => hl.ProjectUUID == ProjectId)
                .WhereIF(StartTime.HasValue, hl => hl.CreateTime >= StartTime)
                .WhereIF(EndTime.HasValue, hl => hl.CreateTime <= EndTime)
                .Select(hl => new
                {
                    hl.UUID,
                    hl.Title,
                    hl.Image,
                    hl.CreateTime
                })
                .ToList();
            return Success(res);
        }

        /// <summary>
        /// 获取家书列表，家书和目录一并返回  add by tianjh 20250802
        /// </summary>
        /// <param name="ProjectUUUID"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public ResponseModel GetHomeLetterList(Guid? ProjectUUUID, DateTime? StartTime, DateTime? EndTime)
        {
            try
            {
                var homeLettersList = db.Queryable<HomeLetters, Projects>((hl, p) => new object[]
                {
                    JoinType.Left, hl.ProjectUUID == p.UUID
                })
                .WhereIF(ProjectUUUID.HasValue, hl => hl.ProjectUUID == ProjectUUUID)
                .WhereIF(StartTime.HasValue, hl => hl.CreateTime >= StartTime)
                .WhereIF(EndTime.HasValue, hl => hl.CreateTime <= EndTime)
                .OrderBy(hl => hl.CreateTime, OrderByType.Desc)
                .Select((hl, p) => new
                {
                    hl.UUID,
                    hl.Title,
                    hl.Content,
                    hl.Image,
                    hl.CreateTime,
                    hl.ProjectUUID,
                    p.ProjectName
                })
                .ToList();

                var parentUuids = homeLettersList.Select(h => h.UUID).ToList();
                var allSubs = parentUuids.Any() ? db.Queryable<HomeLettersSub>().Where(s => parentUuids.Contains(s.ParentUUID)).ToList() : new List<HomeLettersSub>();
                var result = homeLettersList.Select(h =>
                {
                    var dto = new HomeLettersDto();
                    dto.UUID = h.UUID;
                    dto.ProjectName = h.ProjectName;
                    dto.ProjectUUID = h.ProjectUUID;
                    dto.Title = h.Title;
                    dto.Image = h.Image;
                    dto.Content = h.Content;
                    dto.CreateTime = h.CreateTime;
                    dto.HomeLettersSubs = allSubs.Where(s => s.ParentUUID == h.UUID).OrderBy(s => s.CreateTime).ToList();
                    return dto;
                }).ToList();

                return Success(result);
            }
            catch (Exception ex)
            {
                return Error($"获取家书列表失败 {ex.Message}");
            }
        }

        /// <summary>
        /// 获取家书目录
        /// </summary>
        /// <param name="homeLettersId"></param>
        /// <returns></returns>
        public ResponseModel GetHomeLettersCatalogue(Guid homeLettersId)
        {
            // 一次性查询主表与子表（减少数据库往返）
            var homeLetters = db.Queryable<HomeLetters>().First(x => x.UUID == homeLettersId);
            if (homeLetters == null)
            {
                return Error("未找到对应的家书信息");
            }

            var subItems = db.Queryable<HomeLettersSub>()
                             .Where(x => x.ParentUUID == homeLettersId)
                             .ToList();

            var result = new HomeLettersDetailsModel
            {
                UUID = homeLetters.UUID,
                Title = homeLetters.Title,
                Image = homeLetters.Image,
                CreateTime = homeLetters.CreateTime,
                HomeLettersSubs = subItems
            };

            return Success(result);
        }


        /// <summary>
        /// 获取家书内容
        /// </summary>
        /// <param name="HomeLettersId"></param>
        /// <returns></returns>
        public ResponseModel GetHomeLettersDetails(Guid HomeLettersId)
        {
            var res = db.Queryable<HomeLetters>()
                .Where(hl => hl.UUID == HomeLettersId)
                .Select(hl => new
                {
                    hl.UUID,
                    hl.Title,
                    hl.Image,
                    hl.Content,
                    hl.CreateTime
                })
                .ToList()
                ;
            return Success(res);
        }





    }
}
